/**
 * 
 */
package com.dalonedrow.engine.ui.base.border;

import com.dalonedrow.engine.sprite.impl.BasicQuadSpriteObject;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;

/**
 * Abstract class for implementing UI borders comprised of a single RGBA
 * background color.
 * @author DaLoneDrow
 */
public class Abstract1ColorBorder extends GuiBorder {
	/** the middle sprite. */
	private BasicQuadSpriteObject	middle;
	/** the x-offset for the middle graphic. */
	private int						middleInsetX;
	/** the y-offset for the middle graphic. */
	private int						middleInsetY;
	/**
	 * flag indicating whether supplied offsets are used for middle background.
	 */
	private boolean					useOffsets;
	/**
	 * Creates a new instance of {@link Abstract1ColorBorder}.
	 * @param insets the border insets
	 * @param background the background color
	 */
	Abstract1ColorBorder(final int[] insets,
			final float[] background) {
		this(insets, background, -1, -1);
	}
	/**
	 * Creates a new instance of {@link Abstract1ColorBorder}.
	 * @param insets the border insets
	 * @param background the background color
	 * @param xOffset the x-offset for the middle background
	 * @param yOffset the y-offset for the middle background
	 */
	Abstract1ColorBorder(final int[] insets,
			final float[] background, final int xOffset, final int yOffset) {
		final int three = 3, four = 4, eight = 8;
		if (insets == null
				|| insets != null
						&& insets.length != four) {
			super.setInsets(
					eight,
					eight,
					eight,
					eight
					);
		} else {
			super.setInsets(insets);
		}
		if (background == null
				|| background != null
						&& background.length != four) {
			JOGLErrorHandler.getInstance().fatalError(new Exception(
					"4-color background is needed."));
		}
		middleInsetX = xOffset;
		middleInsetY = yOffset;
		try {
			// sprite images don't get set until the preferred size is set
			if (middleInsetX != -1
					&& middleInsetY != -1) {
				useOffsets = true;
			}
			if (middleInsetX == -1) {
				middleInsetX = 0;
			}
			if (middleInsetY == -1) {
				middleInsetY = 0;
			}
			// middle
			middle = new BasicQuadSpriteObject();
			middle.setRed(background[0]);
			middle.setGreen(background[1]);
			middle.setBlue(background[2]);
			middle.setAlpha(background[three]);
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(middle);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void setPosition(final double x, final double y,
			final double z) {
		super.setPosition(x, y, z);
		middle.setPosition(x + middleInsetX, y + middleInsetY, z);
		middle.clearFloatStore();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		try {
			// set middle position and width
			if (useOffsets) {
				middle.setWidth(width - middleInsetX * 2);
				middle.setHeight(height - middleInsetY * 2);
			} else {
				middle.setWidth(width);
				middle.setHeight(height);
			}
			if (super.getPosition() != null) {
				middle.setPosition(
						super.getPosition().getX() + middleInsetX,
						super.getPosition().getY() + middleInsetY,
						super.getPosition().getZ()
						);
				middle.clearFloatStore();
			}
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
